

*-------------------------------------------------------------------------------
* append ISR data
*-------------------------------------------------------------------------------

* combine two parts of 2019
import delimited "data/inputs/isr/2019-ISR-Jan-Jun.csv", stringcols(_all) clear
tempfile isr2019a
save `isr2019a'

import delimited "data/inputs/isr/2019-ISR-Jul-Dec.csv", stringcols(_all) clear
append using `isr2019a'

tempfile isr2019
save `isr2019'

* other years
foreach i of numlist 16/18 20/21 {
	import delimited "data/inputs/isr/20`i'-ISR.csv", stringcols(_all) clear
	tempfile isr20`i'
	save `isr20`i''
}

*append
clear
forval i = 16/21 {
	append using `isr20`i''
}
	
	
*-------------------------------------------------------------------------------
* dates
*-------------------------------------------------------------------------------

* date (two formats)
split contact_date
gen double date = date(contact_date1, "MDY")
replace date = date(contact_date1, "DMY") if date == .
qui su date
assert `r(N)' == _N
format date %td
drop contact_date1 contact_date2

* month 
gen month = mofd(date)
qui su month
assert `r(N)' == _N
format month %tm
	
	
*-------------------------------------------------------------------------------
* save
*-------------------------------------------------------------------------------

* note we are only using ISR data for pedestrian stops
keep if (vehicle_involved == "N")

* counts
gen isr_count_pedestrian = 1

* get race counts overall
gen isr_black_pedestrian = (race_code_cd == "BLK")
gen isr_hispanic_pedestrian = (race_code_cd == "WWH")

* collapse
collapse (sum) isr_count isr_black isr_hispanic, by(district beat month)

* beat numbers
replace beat = "0" + beat if length(beat) == 3
drop district

* full panel
destring beat, gen(beat_num)
tsset beat_num month
tsfill, full
replace beat = beat[_n-1] if beat == "" & beat_num == beat_num[_n-1]
gsort + beat_num - beat
replace beat = beat[_n-1] if beat == "" & beat_num == beat_num[_n-1]
sort beat month
drop beat_num

* missings are zeros here
foreach var of varlist isr_* {
	replace `var' = 0 if `var' == .
	}

* save 
save "data/outputs/ISRpedestrian_beat.dta", replace


* End
